<?php

namespace App\Console\Commands;

use App\Jobs\TempPush;
use App\Models\Appoint;
use App\Models\FamilyEdu;
use App\Models\Hospital;
use App\Models\HospitalAdministrator;
use App\Models\HospitalAdminUserHospital;
use App\Models\old\Article;
use App\Models\old\ArticleInfo;
use App\Models\old\DoctorHospital;
use App\Models\old\HospitalAdminRoleUsers;
use App\Models\old\HospitalAdminUser;
use App\Models\old\OldDoctor;
use App\Models\old\OldHospital;
use App\Models\old\OldHospitals;
use App\Models\old\UserLogin;
use App\Models\TempPushRecord;
use App\Wechat\MyAccessToken;
use App\Wechat\Temp;
use Illuminate\Console\Command;

class migrationDoctors extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migration:doctors';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $doctors = OldDoctor::query()->get();
        foreach ($doctors as $v){
            $doctor_id = OldHospital::query()->where('hospitalid',$v->hospitalid)->value('userid');
            $phone = UserLogin::query()->where('userid',$v->userid)->where('type',1)->value('phone');
            if(!$phone || !$doctor_id)
                continue;
            $row['id'] = $v->userid;
            $row['username'] = $phone;
            $row['name'] = $v->name;
            $row['hospital_id'] = $doctor_id;
            $row['phone_hospital_id'] = $doctor_id;
            $rs['role_id'] = 1;
            $rs['user_id'] = $v->userid;
            $da[] = $rs;
            $data[] = $row;
        }
        HospitalAdminUser::query()->upsert($data,['id']);
        HospitalAdminRoleUsers::query()->upsert($da,['role_id','user_id']);

        $doctorHospital = DoctorHospital::query()->get();
        foreach ($doctorHospital as $v){
            $doctor_id = OldHospital::query()->where('hospitalid',$v->hospitalid)->value('userid');
            $hospitalAdminUsersHospital = HospitalAdminUserHospital::query()->where('hospital_admin_users_id',$v->doctorid)->where('hospital_id',$doctor_id)->first();
            if(!$hospitalAdminUsersHospital && $doctor_id)
            {
                $hospitalAdminUsersHospital = new HospitalAdminUserHospital();
                $hospitalAdminUsersHospital->hospital_admin_users_id = $v->doctorid;
                $hospitalAdminUsersHospital->hospital_id =$doctor_id;
                $hospitalAdminUsersHospital->save();
            }
        }



        return 0;
    }
}
